#pragma once
#include "protocol.hpp"
#include <string>
#include <functional>
using TaskCallback = std::function<Response(Request*)>;
Response Calc(Request* req);
class Task
{
public:
    Task(Request* req = nullptr, TaskCallback callback = Calc)
        :_req(req)
        ,_callback(callback)
    {}
    
    Response Run()
    {
        TaskCallback callback = _callback;
        return callback(_req);
    }

private:
    TaskCallback _callback;
    Request* _req;
};


Response Calc(Request* req)
{
    int a = req->GetA();
    int b = req->GetB();
    std::string op = req->GetOp();
    int result = 0;
    int status = 0;
    if(op == "+")
    {
        result = a + b;
    }
    else if(op == "-")
    {
        result = a - b;
    }
    else if(op == "*")
    {
        result = a * b;
    }
    else if(op == "/")
    {
        if(b == 0)
        {
            status = -1;
        }
        else
        {
            result = a / b;
        }
    }
    else if(op == "%")
    {
        if(b == 0)
        {
            status = -1;
        }
        else
        {
            result = a % b;
        }
    }
    return Response(result, status);
}